<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
	<head>
		<title>号码组管理</title>

		<link rel="stylesheet"
			href="${ctx}/static/plugin/bootstrap/css/bootstrap.min.css" />
		<link rel="stylesheet" href="${ctx}/static/plugin/style.css" />
		<style type="text/css">
span {
	display: inline-block;
}

span label {
	display: inline-block;
}

.controls label {
	display: inline-block;
}
</style>
		<script>
$(document).ready(function() {
	var actionId = '${actionId}';
	if (actionId == 1) {
		$("input:radio[value=1]").attr('checked', 'true');
	}
	getFrameHeight();
		
});
function validateForm() {
	return $("#inputForm").validate( {
		rules : {
			resourcesName : {
				required : true,
				rangelength : [ 1, 50 ]
			},
			resourcesDesc : {
				required : true,
				rangelength : [ 1, 512 ]
			},
			file : "required"
		},
		messages : {
			resourcesName : {
				required : "资源名不能为空！",
				rangelength : jQuery.format("资源名位数必须在{0}到{1}字符之间！")
			},
			description : {
				required : "资源描述不能为空！",
				rangelength : jQuery.format("资源描述位数必须在{0}到{1}字符之间！")
			},
			file : "请上传文件"
		}
	}).form();
}

function doSubmit() {
	if (validateForm()) {
		var actionId = '${actionId}';
		/*
		if (!checkRadio()) {
			alert("类型为必填项,请选择");
			return false;
		}
		*/
		if (actionId == 1) {
			document.inputForm.action = "${ctx}/cases/resources/save/";
		}
		document.inputForm.submit();
	}
}

function resetFileInput(file) {
	file.after(file.clone().val(""));
	file.remove();
}

function in_array(needle, haystack) {
	var type = typeof needle;
	if (type == 'string' || type == 'number') {
		for ( var i in haystack) {
			if (haystack[i] == needle) {
				return true;
			}
		}
	}
	return false;
}

function setImagePreview(fileObj, previewObj, localImg) {
	var rightFileType = new Array("jpg","gif","jpeg","JPG");
	var fileType = $("#"+fileObj).val().substring($("#"+fileObj).val().lastIndexOf(".") + 1);  
	if (!in_array(fileType,rightFileType)) {  
		alert("只支持jpg,gif,jpeg格式图片上传！");
		resetFileInput($("#"+fileObj));
		return;
	}
	var docObj=document.getElementById(fileObj);
	var imgObjPreview=document.getElementById(previewObj);
	if(docObj.files && docObj.files[0]){
		imgObjPreview.style.display = 'block'; 
		imgObjPreview.style.width = '80px';       
		imgObjPreview.style.height = '80px';  
		var reader = new FileReader();
		reader.onload = function(evt){ 
			imgObjPreview.src = evt.target.result;
		}
		reader.readAsDataURL(docObj.files[0]);
	}else{        
		//IE下，使用滤镜        
		docObj.select();
		var imgSrc = document.selection.createRange().text;
		var localImagId = document.getElementById(localImg);        
		//必须设置初始大小       
		localImagId.style.width = "80px";
		localImagId.style.height = "80px";        
		//图片异常的捕捉，防止用户修改后缀来伪造图片       
		try{
			localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
		}catch(e){
			alert("您上传的图片格式不正确，请重新选择!");
			return false;
		}        
		imgObjPreview.style.display = 'none';
		document.selection.empty();
	}
	return true;
}

		function checkRadio(){
			var val=$('input:radio[name="resourcesType"]:checked').val(); 
			if(val==null){ 
                return false; 
            } 
           	return true;
		}
	</script>
	</head>

	<body>
		<div>
			<div id="jCrumbs" class="breadCrumb module">
				<ul>
					<li>
						<a href="#"><i class="icon-home" style="margin-top: 2px;"></i>
						</a>
					</li>
					<li>
						资源管理
					</li>
					<li>
						<a href="${ctx}/mms/resources">资源列表</a>
					</li>
					<li>
						资源编辑
					</li>
				</ul>
			</div>
			<div class="row-fluid">
				<div class="span12">
					<div style="float:right">
						<button class="btn" type="button"  onclick="back()">返 回</button>
					</div>	
					<h3 class="heading">
						资源信息
					</h3>
					<div class="row-fluid">
						<div class="span8">
							<form:form id="inputForm" name="inputForm"
								modelAttribute="resources" cssClass="form-horizontal" action=""
								method="post" enctype="multipart/form-data">
								<fieldset>
									<div class="control-group formSep">
										<label for="u_fname" class="control-label">
											<font color="red">*</font>资源名称:
										</label>
										<div class="controls">
											<input type="text" id="resourcesName" name="resourcesName"
												class="input-xlarge" value="${resources.resourcesName}" />
										</div>
									</div>
									<div class="control-group formSep">
										<label for="fileinput" class="control-label">
											<font color="red">*</font>资源信息:
										</label>
										<div class="controls">
											<div data-fileupload="image"
												class="fileupload fileupload-new">
												<div style="width: 80px; height: 80px;" id="localImag"
													class="fileupload-new thumbnail">
													<img id="preview" width=-1 height=-1 style="diplay: none" />
												</div>
												<input type="file" class="input-xlarge" name="file"
													id="file"
													onchange="setImagePreview('file','preview','localImag');" />

											</div>
										</div>
									</div>
									<div class="control-group formSep">
										<label for="u_signature" class="control-label">
											<font color="red">*</font>说明:
										</label>
										<div class="controls">
											<textarea rows="4" id="resourcesDesc" name="resourcesDesc"
												class="input-xlarge">${resources.resourcesDesc}</textarea>
											<span class="help-block">请输入资源说明</span>
										</div>
									</div>
									<div class="control-group">
										<div class="controls">
											<button class="btn btn-gebo" type="button"
												onclick="doSubmit()">
												保 存
											</button>
											<button class="btn" type="reset">
												重 置
											</button>
										</div>
									</div>
								</fieldset>
							</form:form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
